package com.feituan.manager.controller;


import cn.hutool.core.util.RandomUtil;

import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feituan.core.pojo.entity.SysUser;
import com.feituan.core.persistence.service.SysUserService;
import com.feituan.core.pojo.request.LoginReq;
import com.feituan.core.pojo.response.LoginRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.token.TokenServer;
import com.feituan.core.util.SecureUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * (SysUser)表控制层
 *
 * @author xch
 * @since 2024-06-27 14:53:35
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;


    @Resource
    private TokenServer tokenServer;

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param sysUser 查询实体
     * @return 所有数据
     */
    @GetMapping
    public Response<IPage<SysUser>> selectAll(Page<SysUser> page, SysUser sysUser) {
        return Response.success(this.sysUserService.page(page, new QueryWrapper<>(sysUser)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Response<SysUser> selectOne(@PathVariable Serializable id) {
        return Response.success(this.sysUserService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Response<Boolean> insert(@RequestBody SysUser sysUser) {
        // 加密
        String encrypt = SecureUtil.encrypt(sysUser.getPassword());
        sysUser.setPassword(encrypt);
        return Response.success(this.sysUserService.save(sysUser));
    }

    /**
     * 修改数据
     *
     * @param sysUser 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Response<Boolean> update(@RequestBody SysUser sysUser) {
        return Response.success(this.sysUserService.updateById(sysUser));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Response<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return Response.success(this.sysUserService.removeByIds(idList));
    }


    /**
     * 登录
     * @param loginReq loginReq
     */
    @PostMapping("/login")
    public Response<LoginRes> login(LoginReq loginReq) {
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.eq(SysUser::getUsername, loginReq.getUsername());
        // 查询是否存在用户
        SysUser one = this.sysUserService.getOne(qw);
        if (one == null) {
            throw new ServiceException("用户不存在");
        }
        // 加密
        String encrypt = SecureUtil.encrypt(loginReq.getPassword());
        qw.eq(SysUser::getPassword, encrypt);
        SysUser sysUser = this.sysUserService.getOne(qw);
        if (sysUser != null) {
            // 构建token
            String token = tokenServer.buildToken(loginReq.getUsername());
            LoginRes loginRes = new LoginRes();
            loginRes.setToken(token);
            loginRes.setRole(sysUser.getRoleId());
            return Response.success(loginRes);
        } else {
            throw new ServiceException("密码错误");
        }
    }

}

